package com.njtech.pet.dao.example;

import com.njtech.pet.constant.Constant;
import com.njtech.pet.dao.ActionDao;
import com.njtech.pet.entity.Action;
import com.njtech.pet.entity.Bill;
import com.njtech.pet.entity.Item;
import com.njtech.pet.entity.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * @author lxj
 * Date 2020-03-19 15:10
 * Description ActionDao测试类
 */
public class ActionDaoExample {
    static public ApplicationContext context;
    static public ActionDao actionDao;

    @Before
    public void prepare() {
        context = new ClassPathXmlApplicationContext("spring-dao.xml");
        actionDao = (ActionDao) context.getBean("actionDao");
        System.out.println("prepared " + actionDao);
    }


    @Test
    public void testInsertAction() {
        User user = new User();
        user.setId(3);
        Item item = new Item();
        item.setId(3);
        Bill bill = new Bill();
        bill.setId(6);
        Action action = new Action(null, user, null, bill, Constant.Action.ACTION_VIEW.getValue(), null, Constant.Action.STATE_VALID.getValue(), null, "test1");

        int row = actionDao.insert(action);
        System.out.println(row);
    }

    @Test
    public void testUpdateById() {
        Item item = new Item();
        item.setId(6);
        Bill bill = new Bill();
        bill.setId(6);
        Action action = new Action(2, null, item, null, null, null, Constant.Action.STATE_INVALID.getValue(), null, "test2");
        System.out.println(actionDao.updateByActionId(action));
    }

    @Test
    public void testSelectById() {
        System.out.println(actionDao.selectByActionId(5));
    }

    @Test
    public void testSelectGroup() {
        User user = new User();
        user.setId(3);
        List<Action> actions = actionDao.selectByUserAction(user, Constant.Action.ACTION_VIEW.getValue(),null,null,null, "2020-03-19", null);
        for (Action action : actions) {
            System.out.println(action);
        }
    }
}
